<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <button @click="showStore">显示仓库</button>
        <h1>{{this.$store.state.counter}}</h1>
        <button @click="jia">加一</button>
    </div>
</body>
<script src="./node_modules/vue/dist/vue.js"></script>
<!-- 1.引入依赖 -->
<script src="./node_modules/vuex/dist/vuex.js"></script>
<script src="./node_modules/axios/dist/axios.js"></script>
<script>
    console.log(Vuex);
    // 2.创建仓库
    let store = new Vuex.Store({
            // 仓库中的数据
            state: {
                counter: 777
            },
            mutations: {
                increment: function(state, count) {
                    state.counter += count;
                },

            },
            actions: {
                asyncIncrement: function(store) {
                    axios.get("http://linweiqin.cn:8001/statis/admin/2020-10-17/count").then(res => {
                        console.log(res.data.count)
                        store.commit("increment", res.data.count)
                    }).catch(err => console.log(err))
                }
            }
        })
        // Vue.use(Vuex); //会自动调用install 方法  
    let vm = new Vue({
        // 3.配置仓库
        store,
        el: "#app",
        methods: {
            showStore: function() {
                console.log(this.$store)
            },
            jia: function() {
                // this.$store.state.counter++;直接修改，不推荐
                // vuex里面提交修改
                // this.$store.commit("increment")
                this.$store.dispatch("asyncIncrement");
            }
        }
    })
</script>

</html>